<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\admin\controller;


use app\common\Code;
use app\common\model\GoodsCategory;
use app\common\model\GoodsCategoryRelation;

class Goods extends Common
{
    public function list_()
    {
        $this->check_login();
        $param = input();
        $this->assign("param", $param);
        $goods = new \app\common\model\Goods();
        $list_ = $goods->list_($param);
        $this->assign("list_", $list_);
        if (input('export')) {
            goto EXPORT;
        }
        return $this->fetch();
        EXPORT:
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=data.xls");
        return $this->fetch("export_goods_list");
    }

    /**
     * 加载新增/修改商品视图页
     * @return mixed
     */
    public function edit_()
    {
        $this->check_login();
        $goods_id = input("goods_id");
        if($goods_id)
        {
            $goods = \app\common\model\Goods::get($goods_id);
            $this->assign("goods_data",$goods);
        }
        $goods_category_model = new GoodsCategory();
        #获取有序排序的全部分类
        $goods_category = $goods_category_model->get_all_category();
        $arr = [];
        foreach($goods_category as $item)
        {
            $a1['id'] = $item['id'];
            $a1['pid'] = $item['pid'];
            $a1['category_name'] = $item['category_name'];
            $arr[$a1['id']]=$a1;
        }
        $goods_category = $goods_category_model->get_all_sort_List($arr);
        $this->assign("goods_category",$goods_category);
        #获取已选中商品类型
        if($goods_id)
        {
            $goods_category_relation = new GoodsCategoryRelation();
            $selected_goods_category = $goods_category_relation->getListGoodsCategoryRelation($goods_id); #选中的分类
            $selected_category = "";
            foreach($selected_goods_category as $item){
                $selected_category .= $item['category_name'].'/';
            }
            $this->assign("selected_goods_category", $selected_category);
        }
        return $this->fetch();
    }
    /**
     * 商品操作
     */
    public function edit()
    {
        $goods_id = input("goods_id");
        $param = input();
        $result = $this->validate(
            $param,
            [
                'goods_name' => 'require',         #商品名称
                'place_of_origin' => 'require',    #产地
                'image' => 'require',              #商品图片
                'pre_price' => 'require|number|gt:0',  #商品原价
                'price' => 'require|number|gt:0',      #商品现价
                'stock' => 'require|number|egt:0',     #库存
                'weight' => 'require|number|gt:0',     #商品重量
                # 非必须的 seo:优化, goods_category[]:分类，bar_code:编码,content:内容；
            ]);
        if (true !== $result) {
            response_json(Code::PARAM_ERROR, Code::msg(Code::PARAM_ERROR));
        }
        $goods = new \app\common\model\Goods();
        $goods_category_relation = new GoodsCategoryRelation();
        #编辑商品
        $goods_data = $goods->edit_goods($goods_id,$param);
        if (!empty($param['goods_category'][0]) && $param['goods_category'][0] != 0) {
            //商品类型管理
            $goods_category_relation->delGoodsCategoryRelation($goods_data->id); #删除旧的商品分类关系数据
            $goods_category_relation->addListGoodsCategoryRelation($goods_data->id, $param['goods_category']);
        }
        response_json(Code::NONE, Code::msg(Code::NONE));
    }
    /**
     * 删除商品
     */
    public function del()
    {
        $goods_id = input("goods_id");
        $goods = new \app\common\model\Goods();
        $data = $goods->del_goods_by_id($goods_id);
        if($data == true){
            response_json(Code::NONE, "删除成功");
        }else{
            response_json(Code::FAIL, Code::msg(Code::FAIL));
        }
    }
    /**
     * 审核商品视图
     */
    public function goods_examine_()
    {
        $param = input();
        $this->assign("goods_id",$param['goods_id']);
        //获取商品实体
        $goods_model = new \app\common\model\Goods();
        $goods = $goods_model->fetch_by_id($param['goods_id']);
        if (empty($goods)) {
            exit("<h1>商品不存在，请关闭</h1>");
        }
        $this->assign("goods_data",$goods);
        return $this->fetch();
    }

    /**
     * 审核商品
     */
    public function goods_examine()
    {
        $param = input();
        $goods_model = new \app\common\model\Goods();
        $goods_model->goods_examine($param['goods_id'],$param);
        response_json(Code::NONE, Code::msg(Code::NONE));
    }

    /**
     * 商品类型管理
     * @return array|mixed|\think\response\View
     */
    public function goods_category()
    {
        $pid = input("pid") ? input("pid") : 0;
        $deep = input("deep") ? input("deep") : 0;
        $goods_category = new GoodsCategory();
        $data = $goods_category->getCategoryByPid($pid);
        $this->assign("pid", $pid);
        $deep = $deep + 1;
        $this->assign("deep", $deep);
        if ($pid != 0) {
            $this->assign("back", true);
        }
        $this->assign("data", $data);
        return $this->fetch();
    }


    /**
     * 新增/编辑社区商品分类页
     * @return array|mixed|\think\response\View
     */
    public function category_view()
    {
        $param = input();
        $goods_category = new GoodsCategory();
        if (isset($param['id']) && $param['id'] != 0) {
            $data = $goods_category->getCategoryById($param['id']);
            $this->assign("data", $data);
        }
        $this->assign("deep", $param['deep']);
        $this->assign("pid", $param['pid'] ? $param['pid'] : 0);
        return $this->fetch();
    }

    /**
     * 社区商品分类操作
     * @return \think\response\Json
     */
    public function category_operation()
    {
        $param = input();
        if (isset($param['status'])) {
            if ($param['status'] == "on") {
                $param['status'] = 0;
            }
        } else {
            $param['status'] = 1;
        }
        $result = $this->validate(
            $param,
            [
                'id' => 'require|number',
                'category_name' => 'require',
                'status' => 'require|number',
                'deep' => 'require|number',
            ]);
        if (false == $result) {
           response_json(Code::PARAM_ERROR);
        }
        //判断名称是否重复
        $goods_category = new GoodsCategory();
        $category_name = $goods_category->getCategoryByName($param['category_name']);
        if ($category_name == false) {
            response_json(Code::PARAM_ERROR,[],'分类名称不能重复');
        }
        if ($param['id'] == 0) {
            $result = $goods_category->add_goods_category($param);
        } else {
            $result = $goods_category->edit_goods_category($param['id'], $param);
        }
        if ($result == true) {
            response_json(Code::NONE);
        } else {
            response_json(Code::FAIL);
        }
    }

    /**
     * 删除某条分类
     * @return \think\response\Json
     */
    public function del_promotion_goods()
    {
        $param = input();
        $result = $this->validate(
            $param,
            [
                'id' => 'require|number|gt:0',
            ]);
        if (true !== $result) {
            return json([
                "code" => 1001,
                "data" => "参数错误"
            ]);
        }
        $goods_category = new GoodsCategory();
        #判断此分类下是否有子类
        $child_category = $goods_category->getAllCategoryByPid($param['id']);
        if ($child_category) {
            return json([
                "code" => Code::PARAM_ERROR,
                "data" => "此分类下存在子分类,请先删除子分类"
            ]);
        }
        #判断此分类下是否含有商品
        $goods_category_relation = new GoodsCategoryRelation();
        $relation_data = $goods_category_relation->getGoodsListByCategoryId($param['id']);
        if ($relation_data) {
            return json([
                "code" => 1001,
                "data" => "此分类下存在商品,请先删除商品"
            ]);
        }
        $result = $goods_category->del_one_goods_category($param['id']);
        if ($result == true) {
            return json([
                "code" => 0,
                "data" => "成功"
            ]);
        } else {
            return json([
                "code" => 1002,
                "data" => "失败"
            ]);
        }
    }


}